package Exceptions;

import java.util.Scanner;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/13 17:41
 * 实现一个简单的控制台版用户登陆程序,
 * 程序启动提示用户输入用户名密码. 如果用户名密码出错,
 * 使用自定义异常的方式来处理
 */
public class UserLogin {
    Scanner scanner = new Scanner(System.in);
    private String userName = "admin";
    private String passWord = "1234";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        String password = scanner.nextLine();
        UserLogin userLogin = new UserLogin();
        try {
            userLogin.login(userName, password);
        } catch (PasswordException e) {
            e.printStackTrace();
        }

    }

    public void login(String userName, String passWord) throws PasswordException {
        if (!this.userName.equals(userName)) {
            throw new UserNameException("用户名错误！");
        }
        if (!this.passWord.equals(passWord)) {
            //受查异常，产生异常对象之后需要显示处理
            throw new PasswordException("密码错误！");
        }

    }
}

//用户名异常 非受查异常
class UserNameException extends RuntimeException {
    public UserNameException(String msg) {
        super(msg);
    }
}

//密码异常，受查异常,显示处理：（1，try catch 2，throws）
class PasswordException extends Exception {
    public PasswordException(String msg) {
        super(msg);
    }
}